package br.ufrj.dcc.trabxml.util;

import java.util.Map;
import java.util.TreeMap;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Properties {
    
    private final Map<String,String> map = new TreeMap<String,String>();
    
    public static final Properties instance = new Properties();
    
    private Properties() {
        super();
        refresh();
    }
    
    public String getProperty(String id) {
        return map.get(id);
    }
    
    public void setProperty(String id, String value) {
        map.put(id, value);
    }
    
    public void refresh() {
        map.clear(); 
        String xml = TrabUtil.readTextFile("resources/config.xml");
        if (xml == null) {
            xml = "";
        }
        if (xml.equals("")) {
            xml = "<empty/>";
        }
        Node node = null;
        try {
            node = XmlUtil.getDocumentRoot(xml);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        NodeList childs = node.getChildNodes();
        for(int i = 0; i < childs.getLength(); i++){
            Node child = childs.item(i);
            String value = XmlUtil.getNodeValue(child);
            if (value != null)
                map.put(child.getNodeName(),value);
        }        
    }

}
